package com.cxg.fairy.ec.main.personal.profile;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.personal.list.ListAdapter;
import com.cxg.fairy.ec.main.personal.list.ListBean;
import com.cxg.fairy.ec.main.personal.list.ListItemType;
import com.cxg.fairy.ec.main.personal.settings.NameDelegate;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by CFairy
 */

public class UserProfileDelegate extends LatteDelegate implements ISuccess{

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @BindView(R2.id.rv_user_profile)
    RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_user_profile;
    }

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        RestClient.builder()
                .url("ms/getMsCenter")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }

    @Override
    public void onSuccess(String response) {

                JSONObject person = JSON.parseObject(response).getJSONObject("data").getJSONObject("person");
                String headIco = person.getString("head_ico");

                final ListBean image = new ListBean.Builder()
                        .setItemType(ListItemType.ITEM_AVATAR)
                        .setId(1)
                        .setImageUrl(headIco)
                        .build();

                final ListBean name = new ListBean.Builder()
                        .setItemType(ListItemType.ITEM_NORMAL)
                        .setId(2)
                        .setText("姓名")
                        .setDelegate(new NameDelegate())
                        .setValue("未设置姓名")
                        .build();

                final ListBean gender = new ListBean.Builder()
                        .setItemType(ListItemType.ITEM_NORMAL)
                        .setId(3)
                        .setText("性别")
                        .setValue("未设置性别")
                        .build();

                final ListBean birth = new ListBean.Builder()
                        .setItemType(ListItemType.ITEM_NORMAL)
                        .setId(4)
                        .setText("生日")
                        .setValue("未设置生日")
                        .build();

                final List<ListBean> data = new ArrayList<>();
                data.add(image);
                data.add(name);
                data.add(gender);
                data.add(birth);

                //设置RecyclerView
                final LinearLayoutManager manager = new LinearLayoutManager(getContext());
                mRecyclerView.setLayoutManager(manager);
                final ListAdapter adapter = new ListAdapter(data);
                mRecyclerView.setAdapter(adapter);
                mRecyclerView.addOnItemTouchListener(new UserProfileClickListener(this));
    }
}
